﻿using System;
using System.Windows;
using System.Windows.Controls;
using DevExpress.Xpf.Grid;
using QueryResource.ViewModel;

namespace DevControlLibrary.UI.Comparsion
{

    /// <summary>
    /// 用于行变色
    /// </summary>
    public class TotalCellTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            GridCellData cellData = item as GridCellData;
            if (cellData != null)
            {
                try
                {
                    bool isRed = false;
                    ColumnComparisonEntity columnInfo = cellData.RowData.Row as ColumnComparisonEntity;
                    if (columnInfo != null)
                    {
                        string currentField = cellData.Column.FieldName;
                       
                        switch (currentField)
                        {
                            case "DataType":
                                isRed = (columnInfo.ComparisionInfo & ColumnComparisonInfo.数据类型不相等) ==
                                        ColumnComparisonInfo.数据类型不相等;
                                break;
                            case "IsNullable":
                                isRed = (columnInfo.ComparisionInfo & ColumnComparisonInfo.可空类型不相等) ==
                                        ColumnComparisonInfo.可空类型不相等;
                                break;
                            case "Length":
                                isRed = (columnInfo.ComparisionInfo & ColumnComparisonInfo.长度不相等) ==
                                        ColumnComparisonInfo.长度不相等;
                                break;
                            case "IsPrimaryKey":
                                isRed = (columnInfo.ComparisionInfo & ColumnComparisonInfo.主键不相等) ==
                                        ColumnComparisonInfo.主键不相等;
                                break;
                        }
                    }
                    if (isRed)
                    {
                        FrameworkElement presenter = container as FrameworkElement;
                        if (presenter != null)
                        {
                            return ((GridViewBase)cellData.View).Grid.Resources["totalCellTemplate1"] as DataTemplate;
                        }
                    }
                }
                catch (FormatException) { }
            }
            return base.SelectTemplate(item, container);
        }
    }
}
